﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Meeting.DBUtility;
using Newtonsoft.Json;
using System.Data.SqlClient;
using Meeting.Model;

namespace Meeting.BLL
{
    /// <summary>
    /// 模块作者：许文亭
    /// 模块日期：2013-1-2
    /// 模块功能：平板表Pad 增删查改
    /// </summary>
    public class Pad
    {
        public string errorString = "";

        //获取平板表数据
        private DataSet GetPads(string filter)
        {
            string sql = "Select * From Pad Where 1=1 ";
            if (!string.IsNullOrEmpty(filter)) sql += " And " + filter;
            try
            {
                return DbHelperSQL.Query(sql);
            }
            catch (Exception ex)
            {
                errorString = ex.Message;
                return null;
            }
        }

        /// <summary>
        /// 获取单个实体类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetBy(int id)
        {
            try
            {
                DataSet ds = GetPads(" Id=" + id);
                if (ds == null || ds.Tables[0].Rows.Count == 0) return "";
                Model.Pad model = new Model.Pad();
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Number"] != null && ds.Tables[0].Rows[0]["Number"].ToString() != "")
                {
                    model.Number = ds.Tables[0].Rows[0]["Number"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Mac"] != null && ds.Tables[0].Rows[0]["Mac"].ToString() != "")
                {
                    model.Mac = ds.Tables[0].Rows[0]["Mac"].ToString();
                }
                if (ds.Tables[0].Rows[0]["IsMove"] != null && ds.Tables[0].Rows[0]["IsMove"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsMove"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsMove"].ToString().ToLower() == "true"))
                    {
                        model.IsMove = true;
                    }
                    else
                    {
                        model.IsMove = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["Comment"] != null && ds.Tables[0].Rows[0]["Comment"].ToString() != "")
                {
                    model.Comment = ds.Tables[0].Rows[0]["Comment"].ToString();
                }
                if (ds.Tables[0].Rows[0]["IsActive"] != null && ds.Tables[0].Rows[0]["IsActive"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsActive"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsActive"].ToString().ToLower() == "true"))
                    {
                        model.IsActive = true;
                    }
                    else
                    {
                        model.IsActive = false;
                    }
                }
           
                return JsonConvert.SerializeObject(model);
            }
            catch (Exception ex)
            {
                errorString = ex.Message;
                return "";
            }
        }

        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="filter"></param>
        /// <returns></returns>
        public string GetPage(int pageIndex, int pageSize, string filter)
        {
            int nAllRecord = 0, nAllPages = 0;

            // DataTable dt = DbHelperSQL.GetPage("id", " * ", "rssNews", "", " pubDate Desc ", pageIndex, pageSize, out nAllRecord, out nAllPages);
            DataTable dt = DbHelperSQL.GetPage("Id", " * ", " dbo.Pad ", filter, " Number ", pageIndex, pageSize, out nAllRecord, out nAllPages);
            if (dt == null) return "";
            JsonGrid jr = new JsonGrid();
            jr.total = nAllRecord;
            jr.rows = dt;

            Newtonsoft.Json.Converters.IsoDateTimeConverter timeConverter = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
            timeConverter.DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss";

            return JsonConvert.SerializeObject(jr, timeConverter);
            //return Json.DataTableToJson(dt, nAllRecord);
        }

        /// <summary>
        /// 保存平板信息
        /// </summary>
        public int Add(Meeting.Model.Pad model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Pad(");
            strSql.Append("Number,Mac,IsMove,Comment,IsActive)");
            strSql.Append(" values (");
            strSql.Append("@Number,@Mac,@IsMove,@Comment,@IsActive)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Number", SqlDbType.NVarChar,50),
					new SqlParameter("@Mac", SqlDbType.NVarChar,50),
					new SqlParameter("@IsMove", SqlDbType.Bit,1),
					new SqlParameter("@Comment", SqlDbType.NVarChar,200),
					new SqlParameter("@IsActive", SqlDbType.Bit,1)};
            parameters[0].Value = model.Number;
            parameters[1].Value = model.Mac;
            parameters[2].Value = model.IsMove;
            parameters[3].Value = model.Comment;
            parameters[4].Value = model.IsActive;

            try
            {
                object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
                if (obj == null)
                {
                    return 0;
                }
                else
                {
                    return Convert.ToInt32(obj);
                }
            }
            catch (Exception ex) { errorString = ex.Message; return 0; }
        }

        public Boolean Update(Model.Pad model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Pad set ");
            strSql.Append("Number=@Number,");
            strSql.Append("Mac=@Mac,");
            strSql.Append("IsMove=@IsMove,");
            strSql.Append("Comment=@Comment,");
            strSql.Append("IsActive=@IsActive");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Number", SqlDbType.NVarChar,50),
					new SqlParameter("@Mac", SqlDbType.NVarChar,50),
					new SqlParameter("@IsMove", SqlDbType.Bit,1),
					new SqlParameter("@Comment", SqlDbType.NVarChar,200),
					new SqlParameter("@IsActive", SqlDbType.Bit,1),
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = model.Number;
            parameters[1].Value = model.Mac;
            parameters[2].Value = model.IsMove;
            parameters[3].Value = model.Comment;
            parameters[4].Value = model.IsActive;
            parameters[5].Value = model.Id;


            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string ids)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Pad ");
            strSql.Append(" where Id in (" + ids + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}
